package seabattle;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import seabattle.field.BattleField;
import seabattle.field.Message;
import seabattle.movements.ComputerMovement;
import seabattle.movements.Movement;
import seabattle.movements.WebPlayerMovement;
import seabattle.ship.Coordinates;
import seabattle.ship.RandomShipGenerator;
import seabattle.ship.ShipsGenerator;

@WebServlet("/engine")
public class WebEngine extends HttpServlet {
	private static final long serialVersionUID = 1L;
	final int MAX_LOOP_COUNT = 10000;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String playerShoot = (String) request.getParameter("playerShoot");
		HttpSession session = request.getSession();
		BattleField playerField = (BattleField) session.getAttribute("playerField");
		BattleField computerField = (BattleField) session.getAttribute("computerField");

		boolean accurateShot = true;

		if (!playerShoot.trim().isEmpty() && playerShoot != null) {
			Movement player = new WebPlayerMovement(playerShoot);
			Coordinates coordinates = player.getShot();
			if (coordinates != null) {
				Message shotResult = computerField.catchingShot(coordinates);
				if (shotResult == Message.AGAIN || shotResult == Message.MISS) {
					accurateShot = false;
				} else if (shotResult == Message.HIT) {
					accurateShot = true;
					if (!computerField.checkAliveShips()) {
						session.setAttribute("winner", session.getAttribute("playerName"));
						response.sendRedirect("gameover.jsp");
						return;
					}
				}
			}
		}

		if (!accurateShot) {
			int count = 0;
			do {
				Movement computer = new ComputerMovement();
				Coordinates coordinates = computer.getShot();
				Message shotResult = playerField.catchingShot(coordinates);
				if (shotResult == Message.HIT) {
					if (!playerField.checkAliveShips()) {
						session.setAttribute("winner", "Computer");
						response.sendRedirect("gameover.jsp");
						return;
					}
				}
				if (shotResult == Message.MISS) {
					break;
				}
			} while (count < MAX_LOOP_COUNT);
		}

		session.setAttribute("playerField", playerField);
		session.setAttribute("computerField", computerField);
		response.sendRedirect("fight.jsp");
		return;
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		String playerName = (String) request.getParameter("playerName");
		if (playerName.trim().isEmpty() || playerName == null) {
			response.sendRedirect("login.jsp");
			return;
		}
		session.setAttribute("playerName", playerName);
		BattleField playerField = new BattleField();
		BattleField computerField = new BattleField();
		ShipsGenerator shipGenerator = new ShipsGenerator(new RandomShipGenerator());
		shipGenerator.generateOn(playerField);
		shipGenerator.generateOn(computerField);
		computerField.setVisible(false);
		session.setAttribute("playerField", playerField);
		session.setAttribute("computerField", computerField);
		response.sendRedirect("fight.jsp");
		return;
	}

}
